[[...path]].page.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import React, { useEffect } from 'react';
  2. import { pagePathUtils } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import Head from 'next/head';
  7. import { useRouter } from 'next/router';
  8. import { PageAlerts } from '~/components/PageAlert/PageAlerts';
  9. // import { PageComments } from '~/components/PageComment/PageComments';
  10. // import { useTranslation } from '~/i18n';
  11. import { CrowiRequest } from '~/interfaces/crowi-request';
  12. // import { renderScriptTagByName, renderHighlightJsStyleTag } from '~/service/cdn-resources-loader';
  13. // import { useIndentSize } from '~/stores/editor';
  14. // import { useRendererSettings } from '~/stores/renderer';
  15. // import { EditorMode, useEditorMode, useIsMobile } from '~/stores/ui';
  16. import { IPageWithMeta } from '~/interfaces/page';
  17. import { serializeUserSecurely } from '~/server/models/serializers/user-serializer';
  18. import { useSWRxCurrentPage, useSWRxPageInfo } from '~/stores/page';
  19. import loggerFactory from '~/utils/logger';
  20. import Xss from '~/services/xss';
  21. // import { isUserPage, isTrashPage, isSharedPage } from '~/utils/path-utils';
  22. // import GrowiSubNavigation from '../client/js/components/Navbar/GrowiSubNavigation';
  23. // import GrowiSubNavigationSwitcher from '../client/js/components/Navbar/GrowiSubNavigationSwitcher';
  24. // import DisplaySwitcher from '../client/js/components/Page/DisplaySwitcher';
  25. import { BasicLayout } from '../components/BasicLayout';
  26. // import { serializeUserSecurely } from '../server/models/serializers/user-serializer';
  27. // import PageStatusAlert from '../client/js/components/PageStatusAlert';
  28. import {
  29. useCurrentUser, useCurrentPagePath,
  30. useOwnerOfCurrentPage,
  31. useIsForbidden, useIsNotFound, useIsTrashPage, useShared, useShareLinkId, useIsSharedUser, useIsAbleToDeleteCompletely,
  32. useAppTitle, useSiteUrl, useConfidential, useIsEnabledStaleNotification,
  33. useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsMailerSetup,
  34. useAclEnabled, useHasSlackConfig, useDrawioUri, useHackmdUri, useMathJax, useNoCdn, useEditorConfig, useCsrfToken,
  35. useCurrentPageId
  36. } from '../stores/context';
  37. import { useXss } from '../stores/xss'
  38. import { CommonProps, getServerSideCommonProps, useCustomTitle } from './commons';
  39. import { PageModel } from '~/server/models/page';
  40. import { isValidObjectId } from 'mongoose';
  41. // import { useCurrentPageSWR } from '../stores/page';
  42. const logger = loggerFactory('growi:pages:all');
  43. const { isUsersHomePage, isTrashPage: _isTrashPage } = pagePathUtils;
  44. type Props = CommonProps & {
  45. currentUser: string,
  46. pageWithMetaStr: string,
  47. // pageUser?: any,
  48. // redirectTo?: string;
  49. // redirectFrom?: string;
  50. // shareLinkId?: string;
  51. isForbidden: boolean,
  52. isNotFound: boolean,
  53. // isAbleToDeleteCompletely: boolean,
  54. isSearchServiceConfigured: boolean,
  55. isSearchServiceReachable: boolean,
  56. // isMailerSetup: boolean,
  57. // isAclEnabled: boolean,
  58. // hasSlackConfig: boolean,
  59. // drawioUri: string,
  60. // hackmdUri: string,
  61. // mathJax: string,
  62. // noCdn: string,
  63. // highlightJsStyle: string,
  64. // isAllReplyShown: boolean,
  65. // isContainerFluid: boolean,
  66. // editorConfig: any,
  67. // isEnabledStaleNotification: boolean,
  68. // isEnabledLinebreaks: boolean,
  69. // isEnabledLinebreaksInComments: boolean,
  70. // adminPreferredIndentSize: number,
  71. // isIndentSizeForced: boolean,
  72. };
  73. const GrowiPage: NextPage<Props> = (props: Props) => {
  74. // const { t } = useTranslation();
  75. const router = useRouter();
  76. const { data: currentUser } = useCurrentUser(props.currentUser != null ? JSON.parse(props.currentUser) : null);
  77. // commons
  78. useAppTitle(props.appTitle);
  79. useSiteUrl(props.siteUrl);
  80. useXss(new Xss())
  81. // useEditorConfig(props.editorConfig);
  82. useConfidential(props.confidential);
  83. useCsrfToken(props.csrfToken);
  84. // page
  85. useCurrentPagePath(props.currentPathname);
  86. // useOwnerOfCurrentPage(props.pageUser != null ? JSON.parse(props.pageUser) : null);
  87. // useIsForbidden(props.isForbidden);
  88. // useNotFound(props.isNotFound);
  89. // useIsTrashPage(_isTrashPage(props.currentPagePath));
  90. // useShared(isSharedPage(props.currentPagePath));
  91. // useShareLinkId(props.shareLinkId);
  92. // useIsAbleToDeleteCompletely(props.isAbleToDeleteCompletely);
  93. // useIsSharedUser(props.currentUser == null && isSharedPage(props.currentPagePath));
  94. // useIsEnabledStaleNotification(props.isEnabledStaleNotification);
  95. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  96. useIsSearchServiceReachable(props.isSearchServiceReachable);
  97. // useIsMailerSetup(props.isMailerSetup);
  98. // useAclEnabled(props.isAclEnabled);
  99. // useHasSlackConfig(props.hasSlackConfig);
  100. // useDrawioUri(props.drawioUri);
  101. // useHackmdUri(props.hackmdUri);
  102. // useMathJax(props.mathJax);
  103. // useNoCdn(props.noCdn);
  104. // useIndentSize(props.adminPreferredIndentSize);
  105. // useRendererSettings({
  106. // isEnabledLinebreaks: props.isEnabledLinebreaks,
  107. // isEnabledLinebreaksInComments: props.isEnabledLinebreaksInComments,
  108. // adminPreferredIndentSize: props.adminPreferredIndentSize,
  109. // isIndentSizeForced: props.isIndentSizeForced,
  110. // });
  111. // const { data: editorMode } = useEditorMode();
  112. let pageWithMeta: IPageWithMeta | undefined;
  113. if (props.pageWithMetaStr != null) {
  114. pageWithMeta = JSON.parse(props.pageWithMetaStr) as IPageWithMeta;
  115. }
  116. useCurrentPageId(pageWithMeta?.data._id)
  117. useSWRxCurrentPage(undefined, pageWithMeta?.data); // store initial data
  118. useSWRxPageInfo(pageWithMeta?.data._id, undefined, pageWithMeta?.meta); // store initial data
  119. const classNames: string[] = [];
  120. // switch (editorMode) {
  121. // case EditorMode.Editor:
  122. // classNames.push('on-edit', 'builtin-editor');
  123. // break;
  124. // case EditorMode.HackMD:
  125. // classNames.push('on-edit', 'hackmd');
  126. // break;
  127. // }
  128. // if (props.isContainerFluid) {
  129. // classNames.push('growi-layout-fluid');
  130. // }
  131. // if (page == null) {
  132. // classNames.push('not-found-page');
  133. // }
  134. // // Rewrite browser url by Shallow Routing https://nextjs.org/docs/routing/shallow-routing
  135. // useEffect(() => {
  136. // if (props.redirectTo != null) {
  137. // router.push('/[[...path]]', props.redirectTo, { shallow: true });
  138. // }
  139. // // eslint-disable-next-line react-hooks/exhaustive-deps
  140. // }, []);
  141. return (
  142. <>
  143. <Head>
  144. {/*
  145. {renderScriptTagByName('drawio-viewer')}
  146. {renderScriptTagByName('mathjax')}
  147. {renderScriptTagByName('highlight-addons')}
  148. {renderHighlightJsStyleTag(props.highlightJsStyle)}
  149. */}
  150. </Head>
  151. {/* <BasicLayout title={useCustomTitle(props, t('GROWI'))} className={classNames.join(' ')}> */}
  152. <BasicLayout title={useCustomTitle(props, 'GROWI')} className={classNames.join(' ')}>
  153. <header className="py-0">
  154. {/* <GrowiSubNavigation /> */}
  155. GrowiSubNavigation
  156. </header>
  157. <div className="d-edit-none">
  158. {/* <GrowiSubNavigationSwitcher /> */}
  159. GrowiSubNavigationSwitcher
  160. </div>
  161. <div id="grw-subnav-sticky-trigger" className="sticky-top"></div>
  162. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  163. <div id="main" className={`main ${isUsersHomePage(props.currentPathname) && 'user-page'}`}>
  164. <div className="row">
  165. <div className="col grw-page-content-container">
  166. <div id="content-main" className="content-main grw-container-convertible">
  167. <PageAlerts />
  168. {/* <DisplaySwitcher /> */}
  169. DisplaySwitcher<br />
  170. <div id="page-editor-navbar-bottom-container" className="d-none d-edit-block"></div>
  171. {/* <PageStatusAlert /> */}
  172. PageStatusAlert
  173. </div>
  174. </div>
  175. {/* <div className="col-xl-2 col-lg-3 d-none d-lg-block revision-toc-container">
  176. <div id="revision-toc" className="revision-toc mt-3 sps sps--abv" data-sps-offset="123">
  177. <div id="revision-toc-content" className="revision-toc-content"></div>
  178. </div>
  179. </div> */}
  180. </div>
  181. </div>
  182. <footer>
  183. {/* <PageComments /> */}
  184. PageComments
  185. </footer>
  186. </BasicLayout>
  187. </>
  188. );
  189. };
  190. async function injectPageInformation(context: GetServerSidePropsContext, props: Props): Promise<void> {
  191. const req: CrowiRequest = context.req as CrowiRequest;
  192. const { crowi } = req;
  193. const Page = crowi.model('Page');
  194. const { pageService } = crowi;
  195. const { user } = req;
  196. const { currentPathname } = props;
  197. // determine pageId
  198. let pageId;
  199. const pageIdStr = currentPathname.substring(1);
  200. pageId = isValidObjectId(pageIdStr) ? pageIdStr : null;
  201. const result: IPageWithMeta = await pageService.findPageAndMetaDataByViewer(pageId, currentPathname, user, true); // includeEmpty = true, isSharedPage = false
  202. const page = result.data;
  203. if (page == null) {
  204. const count = pageId != null ? await Page.count({ _id: pageId }) : await Page.count({ path: currentPathname }) ;
  205. // check the page is forbidden or just does not exist.
  206. props.isForbidden = count > 0;
  207. props.isNotFound = true;
  208. logger.warn(`Page is ${props.isForbidden ? 'forbidden' : 'not found'}`, currentPathname);
  209. }
  210. await (page as unknown as PageModel).populateDataToShowRevision();
  211. props.pageWithMetaStr = JSON.stringify(result);
  212. }
  213. // async function injectPageUserInformation(context: GetServerSidePropsContext, props: Props): Promise<void> {
  214. // const req: CrowiRequest = context.req as CrowiRequest;
  215. // const { crowi } = req;
  216. // const UserModel = crowi.model('User');
  217. // if (isUserPage(props.currentPagePath)) {
  218. // const user = await UserModel.findUserByUsername(UserModel.getUsernameByPath(props.currentPagePath));
  219. // if (user != null) {
  220. // props.pageUser = JSON.stringify(user.toObject());
  221. // }
  222. // }
  223. // }
  224. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  225. const req: CrowiRequest = context.req as CrowiRequest;
  226. const { crowi } = req;
  227. const {
  228. appService, searchService, configManager, aclService, slackNotificationService, mailService,
  229. } = crowi;
  230. const { user } = req;
  231. const result = await getServerSideCommonProps(context);
  232. // check for presence
  233. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  234. if (!('props' in result)) {
  235. throw new Error('invalid getSSP result');
  236. }
  237. const props: Props = result.props as Props;
  238. await injectPageInformation(context, props);
  239. if (user != null) {
  240. props.currentUser = JSON.stringify(user);
  241. }
  242. props.isSearchServiceConfigured = searchService.isConfigured;
  243. props.isSearchServiceReachable = searchService.isReachable;
  244. // props.isMailerSetup = mailService.isMailerSetup;
  245. // props.isAclEnabled = aclService.isAclEnabled();
  246. // props.hasSlackConfig = slackNotificationService.hasSlackConfig();
  247. // props.drawioUri = configManager.getConfig('crowi', 'app:drawioUri');
  248. // props.hackmdUri = configManager.getConfig('crowi', 'app:hackmdUri');
  249. // props.mathJax = configManager.getConfig('crowi', 'app:mathJax');
  250. // props.noCdn = configManager.getConfig('crowi', 'app:noCdn');
  251. // props.highlightJsStyle = configManager.getConfig('crowi', 'customize:highlightJsStyle');
  252. // props.isAllReplyShown = configManager.getConfig('crowi', 'customize:isAllReplyShown');
  253. // props.isContainerFluid = configManager.getConfig('crowi', 'customize:isContainerFluid');
  254. // props.isEnabledStaleNotification = configManager.getConfig('crowi', 'customize:isEnabledStaleNotification');
  255. // props.isEnabledLinebreaks = configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks');
  256. // props.isEnabledLinebreaksInComments = configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments');
  257. // props.editorConfig = {
  258. // upload: {
  259. // image: crowi.fileUploadService.getIsUploadable(),
  260. // file: crowi.fileUploadService.getFileUploadEnabled(),
  261. // },
  262. // };
  263. // props.adminPreferredIndentSize = configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize');
  264. // props.isIndentSizeForced = configManager.getConfig('markdown', 'markdown:isIndentSizeForced');
  265. return {
  266. props,
  267. };
  268. };
  269. export default GrowiPage;